package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.domain.CertificationBatchDO;
import com.jinzhi.jzweb.service.CertificationBatchService;
import com.jinzhi.jzweb.service.ExaminationBatchService;
import com.jinzhi.jzweb.service.StuSignUpService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ExaminationCostDO;
import com.jinzhi.jzweb.service.ExaminationCostService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-04-19 09:36:13 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/examinationCost")
public class ExaminationCostController extends BaseController {
	@Autowired
	private ExaminationCostService examinationCostService;
	@Autowired
	private CertificationBatchService certificationBatchService;
	@Autowired
	private ExaminationBatchService examinationBatchService;
	@Autowired
	private StuSignUpService stuSignUpService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:examinationCost:examinationCost")
	String ExaminationCost(){
	    return "jzweb/examinationCost/examinationCost";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:examinationCost:examinationCost")
	public Result<Page<ExaminationCostDO>> list(ExaminationCostDO examinationCostDTO){
		// 查询列表数据
		Page<ExaminationCostDO> page = examinationCostService.selectPage(getPage(ExaminationCostDO.class),
					examinationCostService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ExaminationCostDO obj = (ExaminationCostDO) page.getRecords().get(i);
			CertificationBatchDO certificationBatchDO = certificationBatchService.selectById(obj.getCertificationBatchId());
			obj.setCertificationBatchName(certificationBatchDO.getName());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:examinationCost:add")
	String add(){
	    return "jzweb/examinationCost/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:examinationCost:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ExaminationCostDO examinationCost = examinationCostService.selectById(id);
		model.addAttribute("examinationCost", examinationCost);
	    return "jzweb/examinationCost/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:examinationCost:add")
	public Result<String> save( ExaminationCostDO examinationCost){
		examinationCostService.insert(examinationCost);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:examinationCost:edit")
	public Result<String>  update( ExaminationCostDO examinationCost){
		int stuCount = examinationCost.getStuNum();
		examinationCost.setState(1);
		//计算应收款
		examinationCost.setReceivables(stuCount*418.0);
		//计算应返款
		examinationCost.setReturnMoney(stuCount*100.0);
		boolean update = examinationCostService.updateById(examinationCost);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationCost:remove")
	public Result<String>  remove( Long id){
		examinationCostService.deleteById(id);
        return Result.ok();
	}

	@Log("确认收款及返款")
	@PostMapping( "/confirmation")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationCost:confirmation")
	public Result<String>  confirmation( Long id,int type){
		ExaminationCostDO examinationCostDO = examinationCostService.selectById(id);
		//确认收款
		if(type == 0){
			if(examinationCostDO.getState()==2){
				examinationCostDO.setState(3);
			}else{
				return Result.fail("请核对人数后，再确认回款");
			}
		}else{
			//确认返款
			if(examinationCostDO.getState()==3){
				examinationCostDO.setState(4);
			}else{
				return Result.fail("请确认收款后，再确认返款");
			}
		}
		examinationCostService.updateById(examinationCostDO);
		return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationCost:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		examinationCostService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@Log("批量计算")
	@PostMapping( "/calculate")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationCost:calculate")
	public Result<String>  calculate(@RequestParam("ids[]") Long[] ids){
		for(Long id : ids) {
			ExaminationCostDO examinationCostDO = examinationCostService.selectById(id);
			int stuCount = certificationBatchService.selCbatchCount(examinationCostDO.getCertificationBatchId());
			examinationCostDO.setStuNum(stuCount);
			examinationCostDO.setState(1);
			//计算应收款
			examinationCostDO.setReceivables(stuCount*418.0);
			//计算应返款
			examinationCostDO.setReturnMoney(stuCount*100.0);
			examinationCostService.updateById(examinationCostDO);
		}
		return Result.ok("计算完成");
	}

	@Log("批量核对人数")
	@PostMapping( "/checkStuNum")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationCost:checkStuNum")
	public Result<String>  checkStuNum(@RequestParam("ids[]") Long[] ids){
		String msg = "";
		for(Long id : ids) {
			ExaminationCostDO examinationCostDO = examinationCostService.selectById(id);
			if(examinationCostDO.getState()!=1){
				msg = "部分数据核对失败，只能核对状态为“已计算”的数据";
				continue;
			}
			examinationCostDO.setState(2);
			examinationCostService.updateById(examinationCostDO);
		}
		return Result.ok(msg);
	}


}
